#!/usr/bin/env bash
# Copyright 2014 GSI, Inc. All rights reserved.
#
#

TOOL_NAME="$(basename $0)"
eval WORK_DIR=$(dds-user-defaults --key server.work_dir)
#=============================================================================
# ***** DDS NAMED PIPE *****
#=============================================================================
# a dds-slurm communication pipe (log engine)
eval pipe_dir="$WORK_DIR"
pipe="$pipe_dir/.dds_slurm_pipe"
if [[ ! -p $pipe ]]; then
    echo "$TOOL_NAME error: dds-slurm log engine is not running"
    exit 1
fi
#=============================================================================
# ***** LOG *****
#=============================================================================
# function read input from stdin and write output to the stdout
# caller must take care about where come stdin and where go stdout
logMsgPipe() 
{
  while read data
  do
      logMsg "$data" 
  done
}
logMsg()
{
	echo -e "$TOOL_NAME: $1" > $pipe
}

#=============================================================================
# ***** MAIN *****
#=============================================================================

# if sandbox dir is not equal to working dir,
# copy worker package to the sandbox dir
eval RMS_SANDBOX=$(dds-user-defaults --rms-sandbox-dir)
eval WORK_DIR=$(dds-user-defaults --key server.work_dir)
eval WRK_S_L=$(dds-user-defaults --wrkscript)

if [ "$RMS_SANDBOX" != "$WORK_DIR" ]; then
	cp "$WRK_S_L" "$RMS_SANDBOX"
fi

logMsg "Submitting DDS Job on the SLURM cluster..."
JOB_ID=$(sbatch --job-name dds_agent_  $RMS_SANDBOX/job.slurm)
if (( $? != 0 )) ; then
	logMsg "Error: Failed to submit SLURM job ($?)"
	exit $?
fi
logMsg "SLURM: $JOB_ID"
echo $JOB_ID > "$WORK_DIR/.dds_slurm_jobid"

exit 0
